//-------------------------------------------------------------------
//	Copyright (c) 2013-2014  Zhirnov Andrey
//	This file is part of the "UX OpwnGL Framework" project.
//	See copyright notice in "UX_GL_Framework.h".
//-------------------------------------------------------------------

#include "Sample.h"

/*
=================================================
	constructor
=================================================
*/
	CSampleApplication::CSampleApplication ()
	{
	}
	
/*
=================================================
	destructor
=================================================
*/
	CSampleApplication::~CSampleApplication ()
	{
	}
	
/*
=================================================
	OnInit
=================================================
*/
	void CSampleApplication::OnInit ()
	{
		CHECK( soundsys.Init() );

		soundsys.MakeCurrent();
		
		// generate sound
		const uvec2	dim( 1024 );
		const uint	freq = 44000;

		binary_array_t	snd_data;
		snd_data.Resize( dim.Square() * sizeof(uint16) );

		const CLGenerator::MemImage	images[] = { CLGenerator::MemImage( 0, snd_data, cl_img_channel_order::R, cl_img_channel::SNORM_INT16 ) };
		CLGenerator::Run( "cl/simple_sound.cl", null, null, "Main", svec3(0), svec3( dim ), CLGenerator::texture_args_t(), images );


		// create texture
		texHeight = new GLTexture2D();
		texHeight->SetImage( gl_tex_iformat::R16, uvec3(dim), gl_pixel_format::R, gl_pixel::SHORT, snd_data.ptr() );
		texHeight->SetSampler( GLSamplerSrc::Default_Bilinear_Clamp() );

		ShaderLoader::Load( progViewHeight, "shaders/fs_view_height.prg" );


		// play sound
		soundBuffer = new ALSoundBuffer();
		CHECK( soundBuffer->Create( snd_data.ptr(), snd_data.Size(), al_format::MONO_16, freq, 1.0f, 1.0f, true ) );
		CHECK( soundBuffer->CreateSound( sound ) );

		//WARNING( (string() << "sound duration: " << soundBuffer->Time()).cstr() );

		sound->Play();
	}
	
/*
=================================================
	OnFree
=================================================
*/
	void CSampleApplication::OnFree ()
	{
		sound			= null;
		soundBuffer		= null;

		progViewHeight	= null;
		texHeight		= null;
	}
	
/*
=================================================
	Draw
=================================================
*/
	void CSampleApplication::Draw ()
	{
		SMAN()->Viewport( Window()->Descriptor().vSize.To<ivec2>() );
				
		texHeight->Bind(0);
		progViewHeight->Bind();
		DrawPoint();
	}
	
/*
=================================================
	Update
=================================================
*/
	void CSampleApplication::Update ()
	{
		// exit
		if ( Input()->IsKeyClick( e_key::ESCAPE ) )
		{
			Window()->Quit();
			return;
		}
	}
	
/*
=================================================
	OnResize
=================================================
*/
	void CSampleApplication::OnResize ()
	{
	}

/*
=================================================
	Run
=================================================
*/
	bool CSampleApplication::Run ()
	{
		CSampleApplication	app;

		return
			app._Run( "2-SoundGen/data",
					  CWindow::TDescriptor( uvec2(640,480), ivec2(), e_wnd_style::DEF_WINDOWED, 0, false ),
					  GLWindow::TGLDescriptor( e_glinit_flags::DEFAULT_CORE, 32, 24, 0, 8, 4, true, null ),
					  EInitFlags::USE_OPENCL );
	}
